<?php

namespace app\admins\controller;

use think\Controller;
use Util\SysDb;

class Account extends Controller
{
    public function login()
    {
        return $this->fetch();
    }

    public function dologin()
    {
        //echo md5('123456');
        $username = trim(input('param.username'));
        $password = input('param.password');

        if ($username == '') {
            exit(json_encode(['code' => 1, 'msg' => '用户名不能为空']));
        } elseif ($password == '') {
            exit(json_encode(['code' => 1, 'msg' => '密码不能为空']));
        }

        // 验证用户
        $this->db = new SysDb();
        $admin = $this->db->table('admins')->where(['username' => $username])->item();
        if (!$admin) {
            exit(json_encode(['code' => 1, 'msg' => '用户不存在']));
        }
        if (md5($password) != $admin['password']) {
            exit(json_encode(['code' => 1, 'msg' => '密码错误']));
        }
        if ($admin['status'] == 1) {
            exit(json_encode(['code' => 1, 'msg' => '用户已被禁用']));
        }
        if ($admin['gid'] == 1 && $admin['status'] == 0) {
            exit(json_encode(['code' => 0, 'msg' => '登录成功', 'username' => $admin['realname'], 'phone' => $admin['phone'], 'email' => $admin['email'], 'userBirthday' => $admin['userBirthday'], 'userImg' => $admin['userImg'], 'sex' => $admin['sex']]));
        }
        if ($admin['gid'] == 2 && $admin['status'] == 0) {
            exit(json_encode(['code' => 2, 'msg' => '登录成功', 'username' => $admin['realname'], 'phone' => $admin['phone'], 'email' => $admin['email'], 'userBirthday' => $admin['userBirthday'], 'userImg' => $admin['userImg'], 'sex' => $admin['sex']]));
        }
        if ($admin['gid'] == 3 && $admin['status'] == 0) {
            exit(json_encode(['code' => 3, 'msg' => '登录成功', 'username' => $admin['realname'], 'phone' => $admin['phone'], 'email' => $admin['email'], 'userBirthday' => $admin['userBirthday'], 'userImg' => $admin['userImg'], 'sex' => $admin['sex']]));
        }
    }

    public function register()
    {
        return $this->fetch();
    }

    // public function navs()
    // {
    //     $this->db = new SysDb();
    //     $navs = $this->db->table('navs_role')->lists();
    //     $catalog = $this->db->table('navs_catalog')->lists();
    //     $children = $this->db->table('children')->lists();

    //     $t = [];
    //     $k = [];

    //     foreach ($navs as $i) {
    //         foreach ($catalog as $j) {
    //             $c = [];
    //             foreach ($children as $x) {
    //                 if ($j['sid'] == $x['sid']) {
    //                     array_push($c, $x);
    //                     $j['children'] = $c;
    //                 }
    //             }
    //             if ($i['oid'] == $j['oid']) {
    //                 array_push($k, $j);
    //                 $t[$i['role_name']] = $k;
    //             } else {
    //                 $t[$i['role_name']] = null;
    //             }
    //         }
    //     }

    //     exit(json_encode($t));
    // }

    public function doregister()
    {
        //echo md5('123456');
        $username = trim(input('param.username'));
        $pid = trim(input('param.gid'));
        $password = input('param.password');
        $passwords = input('param.passwords');
        $psd = md5($password);
        $data['username'] = trim(input('param.username'));
        $data['password'] = $psd;
        $data['status'] = '0';
        $data['add_time'] = '';
        $data['gid'] = $pid;
        if ($username == '') {
            exit(json_encode(['code' => 1, 'msg' => '用户名不能为空']));
        } elseif ($password == '') {
            exit(json_encode(['code' => 1, 'msg' => '密码不能为空']));
        } elseif ($password != $passwords) {
            exit(json_encode(['code' => 1, 'msg' => '两次密码不相同重新输入']));
        }

        // 用户注册
        $this->db = new SysDb();
        $admin = $this->db->table('admins')->where(['username' => $username])->item();
        if ($admin) {
            exit(json_encode(['code' => 1, 'msg' => '用户名已存在，请重新输入']));
        }
        if ($admin['status'] == 1) {
            exit(json_encode(['code' => 1, 'msg' => '用户名已被禁用，请重新输入']));
        }

        $this->db->table('admins')->insert($data);
        exit(json_encode(['code' => 0, 'msg' => '注册成功']));
    }
}
